using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public enum ClipType
{
    dianji,
    touchu,
    diaodishang,
    ciru,
    qiege,
    jinshupengzhuang,
    jinshudiaoluo,
    dianjifashe,
    quxiao,
    qiehuanchangjin,
    shibai,
    zuihouyiji,
    renwushenji
}
public class AudioManager : MonoSingleton<AudioManager>
{
    [SerializeField]
    private AudioSource BGMSource;
    [SerializeField]
    private List<AudioSource> SESource;
    [SerializeField]
    private AudioSource SESourcePrefab;
    [SerializeField]
    public AudioClip[] audioClips;
    private float bgmPauseTime;
    public override void Init()
    {
        DontDestroyOnLoad(this);
        base.Init();
    }
    public void PlayBGM(AudioClip clip, bool isLoop = false)
    {
        if (BGMSource == null)
        {
            GameObject gameObject = new GameObject("BGM");
            BGMSource = gameObject.AddComponent<AudioSource>();
            DontDestroyOnLoad(BGMSource.gameObject);
        }
        BGMSource.clip = clip;
        BGMSource.Play();
        BGMSource.loop = isLoop;
    }
    public void PauseBGM()
    {
        if (BGMSource != null && BGMSource.isPlaying)
        {
            bgmPauseTime = BGMSource.time; // 记录当前播放位置
            BGMSource.Pause();
        }
    }

    public void ResumeBGM()
    {
        if (BGMSource != null && !BGMSource.isPlaying)
        {
            BGMSource.time = bgmPauseTime;
            BGMSource.Play();
        }
    }

    public void Play(AudioSource source)
    {
        source.Play();
    }
    public void Play(AudioSource source, AudioClip audioClip)
    {
        source.clip = audioClip;
        source.Play();
    }
    public void Stop(AudioSource source)
    {
        source.Stop();
    }
    public void PlayOnShot(AudioSource source, AudioClip clip)
    {
        source.PlayOneShot(clip);
    }
    public void PlayOnShot(AudioClip clip)
    {
        int i;
        for (i = 0; i < SESource.Count; i++)
        {
            if (!SESource[i].isPlaying)
            {
                PlayOnShot(SESource[i], clip);
                break;
            }
        }
        if (i >= SESource.Count)
        {
            PlayOnShot(SESource[0], clip);
        }
    }
    public void PlayOnShot(int clipIndex)
    {
        int i;
        for (i = 0; i < SESource.Count; i++)
        {
            if (!SESource[i].isPlaying)
            {
                PlayOnShot(SESource[i], audioClips[clipIndex]);
                break;
            }
        }
        if (i >= SESource.Count)
        {
            AudioSource newAudioSource = GameObject.Instantiate<AudioSource>(SESourcePrefab, this.transform);
            SESource.Add(newAudioSource);
            PlayOnShot(SESource[SESource.Count - 1], audioClips[clipIndex]);
        }
    }
}
